﻿using System;


namespace Loef
{
	/// <summary>
	/// Inheriting from this to make the inheriting class a Singleton.
	/// This can be used by multiple sub classes.
	/// 
	/// Some resources that went into creating this class:
	/// http://www.c-sharpcorner.com/UploadFile/snorrebaard/GenericSingleton11172008110419AM/GenericSingleton.aspx
	/// http://www.yoda.arachsys.com/csharp/singleton.html
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public abstract class Singleton<T>
	{
		/// <summary>
		/// This nested class contains a static field named s_instance.  Note that each derived class
		/// will have its OWN instance of s_instance.
		/// </summary>
		private static class InternalSingletonCreator
		{
			/// <summary>
			/// Static constructor required for the C# compiler to NOT mark the class as "beforefieldinit".
			/// </summary>
			static InternalSingletonCreator()
			{ }

			private static T CreateInstance()
			{
				T instance = (T) Activator.CreateInstance(typeof (T), true);
				
				return instance;
			}

			internal static readonly T s_instance = CreateInstance();
		}


		public static T Instance
		{
			get
			{
				return InternalSingletonCreator.s_instance;
			}
		}

	}
}
